<?php if ( ! defined("BASEPATH")) exit("No direct script access allowed");
class news extends MX_Controller
{
  public function __construct()
  {
    parent::__construct();

    $this->load->model("home_model");
    $this->load->model("news_model");
  }
  
  public function sort($field = "created", $order = "desc")
  {
    $this->news_model->dataSort($field, $order);
    $this->index();
  }

  public function index($target = 0)
  {
    $arr = $this->news_model->dataIndex($target); 
    $breakCrum = '<a href="'.base_url().'">'.t("Home").'</a> » <span>'.t("news").'</span>';
    $data = array("content" => $this->load->view("news/main", $arr ,true), "breakCrum" => $breakCrum);
    $this->load->view("templates/admin", $data);
  } 
  
  public function find()
  {
    if (!isset($_POST["txt_find"])) return redirect(site_url("news/index"));
    if (isset($_POST["btnReset"]) && $_POST["btnReset"] == "Reset") 
        $this->session->unset_userdata("sm_find_news");
    else {
        $keyword = $this->input->post("txt_find", true);
        if ($keyword != "") {
            notify_set_messages(notify_info(t("Result")." ".$keyword));
            $this->session->set_userdata("sm_find_news", $keyword);
        }
    }
   
    redirect(site_url("news/index"));
  }
  public function filternews_types()
  {
    $id = $_POST["tid"];
    if (isset($_POST["btnReset"])) 
        $this->session->unset_userdata("sm_filter_news_types");
    else 
        $this->session->set_userdata("sm_filter_news_types", $id);
    redirect(site_url("news"));
  }    
  public function add()
  {        
    $arr = $this->news_model->dataAdd();
    $breakCrum = '<a href="'.base_url().'">'.t("Home").'</a> » 
    <a href="/admin/news">'.t("news").'</a> » <span>Add</span>';
    $data = array("content" => $this->load->view("news/add", $arr, true), 
                  "breakCrum" => $breakCrum);
    $this->load->view("templates/admin", $data);
  }    
  
  public function edit($id)
{       
    $arr = $this->news_model->dataEdit($id);
    $breakCrum = '<a href="'.base_url().'">'.t("Home").'</a> » 
    <a href="/admin/news">'.t("news").'</a> » <span>Edit</span>';
    $data = array("content" => $this->load->view("news/edit", $arr, true),
            "breakCrum" => $breakCrum);
    $this->load->view("templates/admin", $data);
  }
  
  public function doAdd()
  {
    if(!$this->news_model->checkForm()) {
        notify_set_messages(notify_error(validation_errors()));
        return $this->add();
    }
    if ($this->news_model->doUpdate()) {
        notify_set_messages(notify_success(t("Record is added")));
        return redirect(site_url("news"));    
    }
      
  }
  
  public function doEdit()
  {        
    if(!$this->news_model->checkForm()) {
        notify_set_messages(notify_error(validation_errors()));
        return $this->edit(decrypt($_POST["idx"]));        
    }
    if ($this->news_model->doUpdate()) {
        notify_set_messages(notify_success(t("Record is updated")));
        redirect(site_url("news"));   
    }
     
  } 
  
  public function del($id)
  {
  
    if ($this->db->delete("news",array("id"=> $id)))
        notify_set_messages(notify_success(t("Record is deleted")));
    else
        notify_set_messages(notify_atten(t("Record not deleted")));    
    redirect(site_url("news"));    
  }        
                
}